package com.itheima.reggie.Listenerr;

import com.alibaba.fastjson.JSON;
import com.itheima.reggie.exception.BaseContext;

import com.itheima.reggie.exception.R;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Component
public class LoginCheck implements HandlerInterceptor {
    @Override
    //管理员登录拦截
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        Long emid =(Long) request.getSession().getAttribute("employee");
        BaseContext.setCurrentId(emid);
        if(emid==null)
        {
            //请求被拦截
            System.out.println("过滤器没放行");
            response.getWriter().write(JSON.toJSONString(R.error("NOTLOGIN")));
            return  false;
        }
        else{
            System.out.println("过滤器放行了");
            return true;
        }
    }
}
